
package com.yanqu.road.dao.impl.cross.banquet;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.auto.CrossBanquetAutoJoinLog;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBanquetAutoJoinDaoImpl extends TempDataObjectDao<CrossBanquetAutoJoinLog> {

    public Map<Long, List<CrossBanquetAutoJoinLog>> getCrossBanquetAutoJoinLogMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_c_banquet_auto_join_log where activityId = ?";
        Map<Long, List<CrossBanquetAutoJoinLog>> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, activityId);

            rs = call.executeQuery();
            while (rs.next()) {
                CrossBanquetAutoJoinLog data = parse(rs);
                if(!res.containsKey(data.getUserId())){
                    res.put(data.getUserId(),new ArrayList<>());
                }
                res.get(data.getUserId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }

    @Override
    protected boolean addData(CrossBanquetAutoJoinLog data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_c_banquet_auto_join_log(activityId,userId,banquetUserId,banquetId,openItemId,joinItemId,integral,popularity,`time`,isDealReward)" +
                " VALUES(?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getBanquetUserId());
            call.setString(i++, data.getBanquetId());
            call.setInt(i++, data.getOpenItemId());
            call.setInt(i++, data.getJoinItemId());
            call.setLong(i++, data.getIntegral());
            call.setLong(i++, data.getPopularity());
            call.setLong(i++, data.getTime());
            call.setBoolean(i++, data.isDealReward());

            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(CrossBanquetAutoJoinLog data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_c_banquet_auto_join_log SET banquetUserId=?,openItemId=?,joinItemId=?,integral=?,popularity=?,`time`=?,isDealReward=? " +
                "where activityId=? AND userId = ? AND banquetId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getBanquetUserId());
            call.setInt(i++, data.getOpenItemId());
            call.setInt(i++, data.getJoinItemId());
            call.setLong(i++, data.getIntegral());
            call.setLong(i++, data.getPopularity());
            call.setLong(i++, data.getTime());
            call.setBoolean(i++, data.isDealReward());

            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getBanquetId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected CrossBanquetAutoJoinLog parseData(ResultSet rs) throws SQLException {
        CrossBanquetAutoJoinLog data = new CrossBanquetAutoJoinLog();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setBanquetUserId(rs.getLong("banquetUserId"));
        data.setBanquetId(rs.getString("banquetId"));
        data.setOpenItemId(rs.getInt("openItemId"));
        data.setJoinItemId(rs.getInt("joinItemId"));
        data.setIntegral(rs.getLong("integral"));
        data.setPopularity(rs.getLong("popularity"));
        data.setTime(rs.getLong("time"));
        data.setDealReward(rs.getBoolean("isDealReward"));
        return data;
    }
}
